﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using TSVNAddin.SVN;

namespace TSVNAddin.Core.Test
{
	[TestFixture]
	public class PathRenamedActionFixture
	{
		[Test]
		public void NotifyRenamedOnFile()
		{ 
			var versionControl = MockRepository.GenerateMock<IVersionControl>();
			var fileStorage = MockRepository.GenerateMock<IFileStorage>();

			PathRenamedAction action = new PathRenamedAction(versionControl, fileStorage);
			action.Execute(@"c:\root\due.txt", @"uno.txt");

			fileStorage.AssertWasCalled(x => x.Move(@"c:\root\due.txt", @"c:\root\uno.txt"));
			versionControl.AssertWasCalled(x => x.Move(@"c:\root\uno.txt", @"c:\root\due.txt"));
		}

		[Test]
		public void NotifyRenamedOnFileWithouExtension()
		{
			var versionControl = MockRepository.GenerateMock<IVersionControl>();
			var fileStorage = MockRepository.GenerateMock<IFileStorage>();

			PathRenamedAction action = new PathRenamedAction(versionControl, fileStorage);
			action.Execute(@"c:\root\due.txt", @"uno");

			fileStorage.AssertWasCalled(x => x.Move(@"c:\root\due.txt", @"c:\root\uno.txt"));
			versionControl.AssertWasCalled(x => x.Move(@"c:\root\uno.txt", @"c:\root\due.txt"));
		}

		[Test]
		public void NotifyRenamedOnDirectory()
		{
			var versionControl = MockRepository.GenerateMock<IVersionControl>();
			var fileStorage = MockRepository.GenerateMock<IFileStorage>();

			PathRenamedAction action = new PathRenamedAction(versionControl, fileStorage);
			action.Execute(@"c:\root\due\", @"uno");

			fileStorage.AssertWasCalled(x => x.Move(@"c:\root\due\", @"c:\root\uno\"));
			versionControl.AssertWasCalled(x => x.Move(@"c:\root\uno\", @"c:\root\due\"));
		}

		[Test]
		public void NotifyRenamedDoNothingOnSameName()
		{
			var versionControl = MockRepository.GenerateMock<IVersionControl>();
			var fileStorage = MockRepository.GenerateMock<IFileStorage>();

			PathRenamedAction action = new PathRenamedAction(versionControl, fileStorage);
			action.Execute(@"c:\root\uno.txt", @"uno.txt");

			fileStorage.AssertWasNotCalled(x => x.Move(@"c:\root\due.txt", @"c:\root\uno.txt"));
			versionControl.AssertWasNotCalled(x => x.Move(@"c:\root\uno.txt", @"c:\root\due.txt"));
		}

		[Test]
		[ExpectedException(typeof(Exception))]
		public void NotifyRenamedOnThrowExceptionRollBackPath()
		{
			var versionControl = MockRepository.GenerateMock<IVersionControl>();
			var fileStorage = MockRepository.GenerateMock<IFileStorage>();

			versionControl.Expect(x => x.Move(@"c:\root\uno.txt", @"c:\root\due.txt")).Throw(new Exception());

			PathRenamedAction action = new PathRenamedAction(versionControl, fileStorage);
			action.Execute(@"c:\root\due.txt", @"uno.txt");

			fileStorage.AssertWasCalled(x => x.Move(@"c:\root\due.txt", @"c:\root\uno.txt"));
			fileStorage.AssertWasCalled(x => x.Move(@"c:\root\uno.txt", @"c:\root\due.txt"));

			versionControl.VerifyAllExpectations();
		}
	}
}
